package com.mall.shopping.services;

import com.mall.shopping.IContentService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dto.NavListResponse;
import com.mall.shopping.dto.PanelContentDto;
import com.mall.shopping.utils.ExceptionProcessorUtils;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @DATE: 29/9/2021  08:36
 * @USER: xiaom
 * @Author: 2232683886@qq.com
 * @Description: 显示导航栏
 */
@Slf4j
@Service
@Component
public class ContentServiceImpl implements IContentService {

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public NavListResponse queryNavList() {

        NavListResponse navListResponse = new NavListResponse();

        try {

            // 查询panelId为0的panelId
            Example example = new Example(PanelContent.class);
            example.createCriteria()
                    .andEqualTo("panelId",0);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);

            // panelContents -> PanelContentDtos
            List<PanelContentDto> panelContentDtos
                    = contentConverter.panelContents2Dto(panelContents);

            // 封装
            navListResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
            navListResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
            navListResponse.setPannelContentDtos(panelContentDtos);

        } catch (Exception e) {
            log.error("ContentServiceImpl.queryNavList occur Exception: " + e);
            ExceptionProcessorUtils.wrapperHandlerException(navListResponse,e);
        }
        return navListResponse;
    }
}
